package com.auto.learning.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.activity.SplashActivity;
import com.auto.learning.bean.AnchorBean;
import com.auto.learning.bean.PlayFragmentChapterBean;
import com.auto.learning.bean.SelectionVo;
import com.auto.learning.bean.UserBaseInfo;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.pay.UserBuyUtils;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.CircleImageView;
import com.auto.learning.view.SharedPreferencesUtils;
import com.baidu.mobstat.StatService;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PlayFragmentChapterAdapter extends BaseAdapter{
	
	private PlayFragmentChapterBean data;
	
	private Context context;
	private Handler handler;
	private int type = 1; // 1:中文 2：英文    默认为中文
	private String bookName;

	public PlayFragmentChapterAdapter(Context context,PlayFragmentChapterBean data, Handler handler, int type,String bookName){
		this.context = context;
		this.data = data;
		this.handler = handler;
		this.type = type;
		this.bookName=bookName;
	}
	
	@Override
	public int getCount() {
		return data.getSelectionVoList().size() + data.getAnchorList().size();
	}

	@Override
	public Object getItem(int position) {
		return 0;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public boolean isPlayView(int position){
		int ids[] = MusicPlayManager.getInstance(context).isPlayingBookId();
		if (ids[0] > 0) {
			if (position < data.getSelectionVoList().size() && data.getSelectionVoList().get(position).getSectionId() == ids[1]) {
				return true;
			}else {
				return false;
			}
		}
		return false;
	}
	
	ChapterViewHolder holder;
	AnchorViewHolder anchorViewHolder;
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View itemView_charpter = null,itemView_anchor = null;
		if (position < data.getSelectionVoList().size()) {
			itemView_charpter = convertView;
			final SelectionVo vo = data.getSelectionVoList().get(position);
			if (itemView_charpter == null || !(itemView_charpter.getTag() instanceof ChapterViewHolder)) {
				itemView_charpter = LayoutInflater.from(context).inflate(R.layout.chapter_adapter, null);
				holder = new ChapterViewHolder();
				holder.tv_title = (TextView) itemView_charpter.findViewById(R.id.tv_title);
				holder.tv_position = (TextView) itemView_charpter.findViewById(R.id.tv_position);
				holder.image_play_status = (ImageView) itemView_charpter.findViewById(R.id.image_play_status);
				holder.linear_right = (LinearLayout) itemView_charpter.findViewById(R.id.linear_right);
				itemView_charpter.setTag(holder);
			} else {
				holder = (ChapterViewHolder) itemView_charpter.getTag();
			}
			if (type == 1) {
				holder.tv_title.setText(vo.getTitle()+"");
			}else if (type == 2){
				holder.tv_title.setText(vo.getTitleEn()+"");
			}
			holder.tv_position.setText((position + 1) + "");
			int ids[] = MusicPlayManager.getInstance(context).isPlayingBookId();
			if (ids[0] == 0 || ids[1] == 0 || ids[1] == vo.getSectionId()) {
				int secId = 0;
				if (ids[1] == 0) 
					secId = (int) SharedPreferencesUtils.getParam(context, HttpRequestHelper.CONTINUE_PLAY_CHAPTERID, 0);
				else
					secId = ids[1];
				if (secId == vo.getSectionId()) {
					holder.tv_title.setTextColor(context.getResources().getColor(R.color.main_green_color));
					holder.tv_position.setTextColor(context.getResources().getColor(R.color.main_green_color));
					if (ids[2] == 0) {
						holder.image_play_status.setImageResource(R.drawable.pause_small);
					}else {
						holder.image_play_status.setImageResource(R.drawable.play_yuedu2);
					}
				}else {
					holder.tv_title.setTextColor(context.getResources().getColor(R.color.main_text_color));
					holder.tv_position.setTextColor(context.getResources().getColor(R.color.main_text_color));
					holder.image_play_status.setImageResource(R.drawable.play_small);
				}
			} else {
				holder.tv_title.setTextColor(context.getResources().getColor(R.color.main_text_color));
				holder.tv_position.setTextColor(context.getResources().getColor(R.color.main_text_color));
				holder.image_play_status.setImageResource(R.drawable.play_small);
			}
			holder.linear_right.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					MusicPlayManager manager = MusicPlayManager.getInstance(context);
					if (((PlayActivity) context).getNeedBuy() == 2&& !TemporaryVariable.downLoadedList.contains(vo.getBookId())) {
						if (manager.getPlayList() != null&& manager.getPlayList().size() > 0) {
							int ids[] = manager.isPlayingBookId();
							if (ids[0] != vo.getBookId()) {
								new UserBuyUtils(((Activity) context), vo.getBookId(), data.getPriceTag(),bookName).buy();
								return;
							}
						} else {
							new UserBuyUtils(((Activity) context), vo.getBookId(), data.getPriceTag(),bookName).buy();
							return;
						}
					}
					StatService.onEvent(context, "LogEventChapterPlayBtnClick","章节后面的播放按钮");
					handler.obtainMessage(20, position, vo.getBookId()).sendToTarget();

				}
			});
			convertView = itemView_charpter;
			if (TextUtils.isEmpty(holder.tv_title.getText())) {
				convertView.setVisibility(View.GONE);
			}else {
				convertView.setVisibility(View.VISIBLE);
			}
		}else {
			itemView_anchor = convertView;
			final AnchorBean anchorBean = data.getAnchorList().get(position - data.getSelectionVoList().size());
			if (itemView_anchor == null || !(itemView_anchor.getTag() instanceof AnchorViewHolder)) {
				itemView_anchor = LayoutInflater.from(context).inflate(R.layout.anchor_adapter, null);
				anchorViewHolder = new AnchorViewHolder();
				anchorViewHolder.image_a_face = (CircleImageView) itemView_anchor.findViewById(R.id.image_a_face);
				anchorViewHolder.name = (TextView) itemView_anchor.findViewById(R.id.name);
				anchorViewHolder.style = (TextView) itemView_anchor.findViewById(R.id.style);
				anchorViewHolder.arce = (TextView) itemView_anchor.findViewById(R.id.arce);
				anchorViewHolder.intro = (TextView) itemView_anchor.findViewById(R.id.intro);
				anchorViewHolder.tv_anchor_favor_number = (TextView) itemView_anchor.findViewById(R.id.tv_anchor_favor_number);
				anchorViewHolder.linear_favor = (LinearLayout) itemView_anchor.findViewById(R.id.linear_favor);
				anchorViewHolder.ratingBar = (RatingBar) itemView_anchor.findViewById(R.id.rat_num);
				anchorViewHolder.im_zan=(ImageView) itemView_anchor.findViewById(R.id.im_zan);
				itemView_anchor.setTag(anchorViewHolder);
			} else {
				anchorViewHolder = (AnchorViewHolder) itemView_anchor.getTag();
			}
			ImageLoader.getInstance().displayImage(anchorBean.getFace(),anchorViewHolder.image_a_face);
			anchorViewHolder.name.setText(anchorBean.getName());
			anchorViewHolder.style.setText(anchorBean.getStyle());
			anchorViewHolder.arce.setText(anchorBean.getArea());
			anchorViewHolder.intro.setText(anchorBean.getIntro());
			anchorViewHolder.tv_anchor_favor_number.setText(anchorBean.getFavorNum()+"");
			anchorViewHolder.ratingBar.setNumStars(anchorBean.getStars());
			if(anchorBean.getIsFavor()==1){
				anchorViewHolder.im_zan.setImageResource(R.drawable.zan);
				anchorViewHolder.tv_anchor_favor_number.setTextColor(context.getResources().getColor(R.color.white));
			}else{
				anchorViewHolder.im_zan.setImageResource(R.drawable.pinlun_zan_green);
				anchorViewHolder.tv_anchor_favor_number.setTextColor(context.getResources().getColor(R.color.main_green_color));
			}
		//	anchorViewHolder.im_zan.setBackgroundDrawable(background);
			anchorViewHolder.linear_favor.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					handler.obtainMessage(9, anchorBean.getId()).sendToTarget();
				}
			});
			anchorViewHolder.image_a_face.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					handler.obtainMessage(12, anchorBean).sendToTarget();
				}
			});
			convertView = itemView_anchor;
		}
		return convertView;
	}
	
	class ChapterViewHolder{
		TextView tv_title,tv_position;
		ImageView image_play_status;
		LinearLayout linear_right;
	}
	
	class AnchorViewHolder{
		CircleImageView image_a_face;
		TextView name,style,arce,intro,tv_anchor_favor_number;
		LinearLayout linear_favor;
		RatingBar ratingBar;
		ImageView im_zan;
	}
	

	public void setType(int type) {
		this.type = type;
	}
	
	public int getType() {
		return type;
	}

}
